package com.htg.util;

import android.graphics.Matrix;

/**
 * 仅支持二维平面坐标系
 */
public class UtilMatrix {

    /**
     * @param sx1 原坐标系 坐标1
     * @param sy1 原坐标系 坐标1
     * @param sx2 原坐标系 坐标2
     * @param sy2 原坐标系 坐标2
     * @param sx3 原坐标系 坐标3
     * @param sy3 原坐标系 坐标3
     * @param mx1 新坐标系 坐标1
     * @param my1 新坐标系 坐标1
     * @param mx2 新坐标系 坐标2
     * @param my2 新坐标系 坐标2
     * @param mx3 新坐标系 坐标3
     * @param my3 新坐标系 坐标3
     * @return
     */
    public static Matrix2D getMatrix2D(double sx1, double sy1, double sx2, double sy2, double sx3, double sy3, double mx1, double my1, double mx2, double my2, double mx3, double my3) {
        //
        double[] ab = calculateAB(sx1, sy1, sx2, sy2, sx3, sy3, mx1, mx2, mx3);
        double a = ab[0];
        double b = ab[1];
        double[] cd = calculateCD(sx1, sy1, sx2, sy2, sx3, sy3, my1, my2, my3);
        double c = cd[0];
        double d = cd[1];

        double e = mx1 - a * sx1 - b * sy1;
        double f = my1 - c * sx1 - d * sy1;

        Matrix2D matrix2D = new Matrix2D();

        matrix2D.setA(a);
        matrix2D.setB(b);
        matrix2D.setC(c);
        matrix2D.setD(d);
        matrix2D.setE(e);
        matrix2D.setF(f);

        return matrix2D;
    }

    private static double[] calculateCD(double sx1, double sy1, double sx2, double sy2, double sx3, double sy3, double my1, double my2, double my3) {
        double p1 = sx1 - sx2;
        double p2 = sy1 - sy2;
        double p3 = sx2 - sx3;
        double p4 = sy2 - sy3;
        //
        double p1ni = p4 / (p1 * p4 - p2 * p3);
        double p2ni = -p2 / (p1 * p4 - p2 * p3);
        double p3ni = -p3 / (p1 * p4 - p2 * p3);
        double p4ni = p1 / (p1 * p4 - p2 * p3);
        //
        double p5 = my1 - my2;
        double p6 = my2 - my3;
        //
        double c = p1ni * p5 + p2ni * p6;
        double d = p3ni * p5 + p4ni * p6;
        return new double[]{c, d};
    }

    private static double[] calculateAB(double sx1, double sy1, double sx2, double sy2, double sx3, double sy3, double mx1, double mx2, double mx3) {
        double p1 = sx1 - sx2;
        double p2 = sy1 - sy2;
        double p3 = sx2 - sx3;
        double p4 = sy2 - sy3;
        //
        double p1ni = p4 / (p1 * p4 - p2 * p3);
        double p2ni = -p2 / (p1 * p4 - p2 * p3);
        double p3ni = -p3 / (p1 * p4 - p2 * p3);
        double p4ni = p1 / (p1 * p4 - p2 * p3);
        //
        double p5 = mx1 - mx2;
        double p6 = mx2 - mx3;
        //
        double a = p1ni * p5 + p2ni * p6;
        double b = p3ni * p5 + p4ni * p6;

        return new double[]{a, b};
    }


    public static class Matrix2D {

        //a,b,c,d是旋转缩放的矩阵元素
        private double a;
        private double b;
        private double c;
        private double d;
        //e,f是平移向量元素
        private double e;
        private double f;

        public void setA(double a) {
            this.a = a;
        }

        public void setB(double b) {
            this.b = b;
        }

        public void setC(double c) {
            this.c = c;
        }

        public void setD(double d) {
            this.d = d;
        }

        public void setE(double e) {
            this.e = e;
        }

        public void setF(double f) {
            this.f = f;
        }

        /**
         * 将原坐标系的坐标 转换为新坐标系的坐标
         *
         * @param x
         * @param y
         * @return
         */
        public double[] mapping(double x, double y) {
            double mx = a * x + b * y + e;
            double my = c * x + d * y + f;
            return new double[]{mx,my};
        }
    }

}
